<?php

class live{

	function __construct(){
		$this->userId    = $GLOBALS['live_conf']['userId'];
		$this->secretKey = $GLOBALS['live_conf']['secretKey'];
		$this->url_pre   = 'http://api.cloudv.haplat.net/live/channelManage/';

		$now = time();
		$this->token = md5($this->secretKey.$this->userId.$now);
		$this->param = array('userId'=>$this->userId,'token'=>$this->token,'timeStamp'=>$now);
	}

	function channel_list(){
		global $cache;
		$url = $this->url_pre.'getChannelList';
		if(!$cache->get('live_channels')){
		    $rs = $this->http_post($url,$this->param);
			$rs = json_decode($rs,true);
		    $live_channels = isset($rs['data']['channelList']) ? $rs['data']['channelList'] : array();
		    $cache->set('live_channels',$live_channels,3600);
		}
		return $cache->get('live_channels');
	}

	function channel_info($pullId=''){
		/*global $cache;
		$this->param['pullId'] = $pullId;
		$url = $this->url_pre.'getChannelConfigure';
		$rs = $this->http_post($url,$this->param);
		$rs = json_decode($rs,true);
		return $rs['data'];
		*/
		global $cache;
		$this->param['pullId'] = $pullId;
		$url = $this->url_pre.'getChannelConfigure';
		if(!$cache->hget('live_channel_info',$pullId)){
		    $rs = $this->http_post($url,$this->param);
			$rs = json_decode($rs,true);
		    $channel_info = isset($rs['data']) ? $rs['data'] : array();
		    $cache->hset('live_channel_info', $pullId, $channel_info);
		}
		return $cache->hget('live_channel_info',$pullId);
	}

	function channel_state($pullId=''){
		$this->param['pullId'] = $pullId;
		$url = $this->url_pre.'getChannelLiveState';
		$rs = $this->http_post($url,$this->param);
		$rs = json_decode($rs,true);
		return $rs['data'];
	}

	function channel_forbidde($pullId=''){
		$this->param['pullId'] = $pullId;
		$url = $this->url_pre.'channelForbidde';
		$rs = $this->http_post($url,$this->param);
		$rs = json_decode($rs,true);
		return $rs['data'];
	}

	function channel_rebrodcast($pullId=''){
		$this->param['pullId'] = $pullId;
		$url = $this->url_pre.'channelReBrodcast';
		$rs = $this->http_post($url,$this->param);
		$rs = json_decode($rs,true);
		return $rs['data'];
	}

	function http_post($url, $param=array(), $headers=array(), $post_file = false, $return = 1)
	{
	    $oCurl = curl_init();
	    if (stripos($url, "https://") !== FALSE) {
	        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
	        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
	        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
	    }
	    if (is_string($param) || $post_file) {
	        $strPOST = $param;
	    } else {
	        $aPOST = array();
	        foreach ($param as $key => $val) {
	            $aPOST[] = $key . "=" . urlencode($val);
	        }
	        $strPOST = join("&", $aPOST);
	    }
	    curl_setopt($oCurl, CURLOPT_URL, $url);
	    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, $return);
	    curl_setopt($oCurl, CURLOPT_POST, true);
	    curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
	    //发送请求头
	    $sendhead = array();
	    foreach($headers as $k=>$v){
	        $sendhead[] = $k.':'.$v;
	    }
	    curl_setopt($oCurl, CURLOPT_HTTPHEADER, $sendhead);
	    $sContent = curl_exec($oCurl);
	    $aStatus = curl_getinfo($oCurl);
	    curl_close($oCurl);
	    if (intval($aStatus["http_code"]) == 200) {
	        return $sContent;
	    } else {
	        return $sContent;
	    }
	}

}